package com.jiayinlending;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.jiayinlending.base.BaseApplication;

/**
 * Created by YueXiaoHui on 2015/4/28.
 * 全局应用配置类，用于保存全局应用配置及访问网络信息
 */
public class AppContext extends BaseApplication {

    public static final String LOGIN_USER_KEY = "login_user_obj";//保存登入用户的key
    private static final String GESTURE_PASSWORD = "GESTURE_PASSWORD";


    private String DEVICE_ID;//设备唯一标识
    private static AppContext instance;

    private boolean login = false; // 登录状态
    private String saveImagePath;// 保存图片路径

    public static boolean isPaySuccess = false;

    public static int pushProductId = 0;
    public static int pushNewsId = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static AppContext instance() {
        return instance;
    }


    /**
     * 初始化
     */

    public String DeviceId() {
        SharedPreferences prefs = this.getSharedPreferences("device_id.xml", 0);
        DEVICE_ID = prefs.getString("device_id", "");
        return DEVICE_ID;
    }

    /**
     * 判断用户是否登入
     *
     * @return
     */
    public boolean isLogin() {
        return login;
    }


    /**
     * 获取App安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }




    public static final String KEY_ACCESSTOKEN_STRING = "KEY_ACCESSTOKEN_STRING";

    /**
     * 获取accesstoken
     *
     * @return
     */
    public static String getAccesstoken() {
        SharedPreferences sharedPreferences = AppContext.getPreferences();
        String accesstoken = sharedPreferences.getString(KEY_ACCESSTOKEN_STRING, "");
        return accesstoken;
    }

    /**
     * 保存accesstoken
     *
     * @param accessToken
     */
    public static void setAccessToken(String accessToken) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(KEY_ACCESSTOKEN_STRING, accessToken);
        apply(editor);
    }

}
